﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Web.group
{
    public partial class shareoutedit : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(Request["phonebookid"]))
            {
                throw new Base.CustomException("缺少参数phonebookid");
            }

            Hashtable phonebook = PhoneBook.Group.PhoneBook.get(Request["phonebookid"]);
            if (phonebook == null)
            {
                throw new Base.CustomException("参数phonebookid不正确");
            }

            Hashtable user = PhoneBook.Users.Users.get(Commons.StrUtil.FormatValue(Session["userid"]));
            if (!user["groupid"].Equals(phonebook["groupid"]))
            {
                throw new Base.CustomException("无权限设置此通讯录");
            }

            SetDivePage(ref GridView1, ref GV);
            if (!IsPostBack)
            {
                QueryData();
            }
        }

        public override void QueryData()
        {
            DataSet ds = PhoneBook.Group.PbShare.list(Request["phonebookid"]);
            //绑定数据集
            GVDataBind(ref GridView1, ref GV, ds);
        }

        protected void btn_save_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox cb_sel = row.FindControl("cb_sel") as CheckBox;
                if (cb_sel == null) continue;
                string groupid = string.Empty;
                if (GridView1.DataKeys.Count > 0)
                {
                    groupid = GridView1.DataKeys[row.RowIndex]["groupid"].ToString();
                    if (cb_sel.Checked)
                        PhoneBook.Group.PbShare.add(groupid, Request["phonebookid"]);
                    else
                        PhoneBook.Group.PbShare.del(groupid, Request["phonebookid"]);
                }
            }
            Hashtable phonebook = PhoneBook.Group.PhoneBook.get(Request["phonebookid"]);
            AddLog("修改了集团通讯录({0})的对外共享规则。", phonebook["phonebookname"]);

            Helper.ShowMessage(u_message, controls.UCMessageType.success, "保存成功");
            QueryData();
        }
    }
}